<%--
  Created by IntelliJ IDEA.
  User: 52203
  Date: 2018/12/29
  Time: 9:21
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <base href="<%=request.getContextPath()%>/"/>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <script src="static/js/jquery-2.1.1.min.js"></script>
    <%@include file="../layUI.jsp"%>


</head>
<body>
<form action="doSmall?action=smallAdd" method="post" id="dataFrm">
    <di id="d1"></di>
    小分类名称<input type="text"  name="smallName" id="smallName" class="notNull" notNull="小分类名称">
    大分类名称<select name="smallBigId" id="smallBigId">
    </select>
    小分类描述<input type="text" name="smallText"class="notNull" notNull="小分类描述">
    <input type="button" value="添加" class="layui-btn" id="sub" onclick="addSms()" >
</form>
<script>
    //初始化数据
    $(function () {
        $.getJSON("/doSmall",{"action":"selSmallBigName"},callback);
        function callback(data) {
            $(data).each(function (i) {
                $("#smallBigId").append(" <option value='"+(i+1)+"'>"+this.bigName+"</option>");
            })
        }

        $("#smallName").blur(function () {
            var smallName= $("#smallName").val();
            $.getJSON("/doSmall?action=checkSamllName",{"smallName":smallName},callback);
            function callback(data) {
                if(data.flag=="true"){
                    alert("已存在相同名称的小分类，请更换！");
                    $("#smallName").val("")
                }
            }
        })
    })
    function addSms() {
        //非空验证
        var flag = true;
        $(".notNull").each(function () {
            var name = $(this).attr("name");
            if($(this).val()==""){
                alert($(this).attr('notNull')+"不能为空");
                flag =false;
                return;
            }
        })
        if(flag){
            var queryString=$("#dataFrm").serialize();
            $.getJSON("/doSmall?action=addSmall",queryString,callback)
            function callback(data) {
                if (data.flag == "true") {
                    location.href="/page/smallclass/SmallQuery.jsp";
                    alert("添加成功!");
                } else {
                    location.href="/page/smallclass/SmallUpdate.jsp";
                    alert("添加失败!");
                }
            }
        }

    }
</script>
</body>
</html>
